﻿// Decompiled with JetBrains decompiler
// Type: TaleWorlds.CampaignSystem.CampaignInformationManager
// Assembly: TaleWorlds.CampaignSystem, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E85F8C15-4DF6-4E9C-A58A-29177E40D07A
// Assembly location: D:\steam\steamapps\common\Mount & Blade II Bannerlord\bin\Win64_Shipping_Client\TaleWorlds.CampaignSystem.dll

using System;
using System.Collections.Generic;
using TaleWorlds.CampaignSystem.LogEntries;
using TaleWorlds.Core;
using TaleWorlds.Library;
using TaleWorlds.SaveSystem;

#nullable disable
namespace TaleWorlds.CampaignSystem
{
  public class CampaignInformationManager
  {
    [SaveableField(10)]
    private List<InformationData> _mapNotices;
    [CachedData]
    private bool _isSessionLaunched;

    private void MapNoticeRemoved(InformationData obj)
    {
      int index1 = -1;
      for (int index2 = 0; index2 < this._mapNotices.Count; ++index2)
      {
        if (obj == this._mapNotices[index2])
          index1 = index2;
      }
      if (index1 < 0)
        return;
      this._mapNotices.RemoveAt(index1);
    }

    public CampaignInformationManager() => this._mapNotices = new List<InformationData>();

    internal void NewLogEntryAdded(LogEntry log)
    {
      if (!this._isSessionLaunched || !(log is IChatNotification chatNotification) || !chatNotification.IsVisibleNotification)
        return;
      InformationManager.DisplayMessage(new InformationMessage()
      {
        Information = chatNotification.GetNotificationText().ToString(),
        Color = Color.FromUint(Campaign.Current.Models.DiplomacyModel.GetNotificationColor(chatNotification.NotificationType))
      });
    }

    private void AddInformationData(InformationData informationData)
    {
      this._mapNotices?.Add(informationData);
      MBInformationManager.AddNotice(informationData);
    }

    internal void RegisterEvents()
    {
      this._isSessionLaunched = true;
      MBInformationManager.OnRemoveMapNotice += new Action<InformationData>(this.MapNoticeRemoved);
    }

    internal void DeRegisterEvents()
    {
      this._isSessionLaunched = false;
      MBInformationManager.OnRemoveMapNotice -= new Action<InformationData>(this.MapNoticeRemoved);
    }

    public void OnGameLoaded()
    {
      this._mapNotices.RemoveAll((Predicate<InformationData>) (t => t == null || !t.IsValid()));
      foreach (InformationData mapNotice in this._mapNotices)
        MBInformationManager.AddNotice(mapNotice);
    }

    public void NewMapNoticeAdded(InformationData informationData)
    {
      this.AddInformationData(informationData);
    }

    public bool InformationDataExists<T>(Func<T, bool> predicate) where T : InformationData
    {
      foreach (InformationData mapNotice in this._mapNotices)
      {
        if (mapNotice is T obj && (predicate == null || predicate(obj)))
          return true;
      }
      return false;
    }

    internal static void AutoGeneratedStaticCollectObjectsCampaignInformationManager(
      object o,
      List<object> collectedObjects)
    {
      ((CampaignInformationManager) o).AutoGeneratedInstanceCollectObjects(collectedObjects);
    }

    protected virtual void AutoGeneratedInstanceCollectObjects(List<object> collectedObjects)
    {
      collectedObjects.Add((object) this._mapNotices);
    }

    internal static object AutoGeneratedGetMemberValue_mapNotices(object o)
    {
      return (object) ((CampaignInformationManager) o)._mapNotices;
    }

    public enum NoticeType
    {
      None,
      WarAnnouncement,
      PeaceAnnouncement,
      ChangeSettlementOwner,
      FortificationIsCaptured,
      HeroChangedFaction,
      BarterAnnouncement,
    }
  }
}
